#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <math.h>


const unsigned int A = 1664525, C = 1013904223;
unsigned int x;
double *trials;

// <editor-fold defaultstate="collapsed" desc="funcoes do metodo">

double max(double a, double b) {
    if (a >= b) return a;
    return b;
}
//calcula media
double mean(double vec[], int m) {
    int i;
    double sum = 0;

    for (i = 0; i < m; i++) {
        sum += vec[i];
    }
    return sum / (double) m;
}
//utilizado para o calculo do numero aleatorio
unsigned int next(unsigned int x) {
    return (x * A + C);
}
//calcula o desvio padrao
double stddev(double vec[], int N, double media) {

    double sum = 0;
    int i;

    for (i = 0; i < N; i++) {
        sum += (vec[i] - media) * (vec[i] - media);
    }

    return (sqrt((1.0 / (double) (N - 1)) * sum));
}
//calcula o tamanho do intervalo de confianca
double confwidth(double stddev, int M) {
    return ( (1.96 * stddev) / (double) sqrt((double) M));
}
//calcula o o minimo do intervalo de confianca
double confmm(double media, double confwidth) {
    return (double) (media - confwidth);
}
//calcula o maximo do intervalo de confianca
double confmax(double media, double confwidth) {
    return (media + confwidth);
}// </editor-fold>

int main(void) {

    // <editor-fold defaultstate="collapsed" desc="inicializar variaveis e leitura da entrada">
    int i, j;

    double S = 1; // valor da ação

    double E = 1; //preço de exercício da opção

    double r = 1; //taxa de juros livre de risco (SELIC)

    double sigma = 1; //volatilidade da ação

    double T = 1.0; // tempo de validade da opção

    double t;
    
    int M = 2; //numero de iterações// 
    
    double mean1, stddev1, confwidth1, confmm1, confmax1; 
    
    scanf("%lf", &S);
    scanf("%lf", &E);
    scanf("%lf", &r);
    scanf("%lf", &sigma);
    scanf("%lf", &T);
    scanf("%d", &M);

    printf("Entradas: %lf %lf %lf %lf %lf %d\n", S, E, r, sigma, T, M);

    trials = (double*) malloc( M * sizeof(double) );
    
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="blackScholes">
    
    //calcula M iteracoes da formula de Black Scholes e armazena no vetor trials
    for (i = 0; i < M; i++) {
        
        x = next(x);
        double randomNumber = (double) x / (1LL<<32);
        
        t = S * exp( (r - ( (0.5) * sigma * sigma))  * T + sigma * sqrt(T) * randomNumber);
     
        trials[i] = exp( (-1) * r * T ) * max(t - E, 0);
    }

    mean1 = mean(trials, M);
    stddev1 = stddev(trials, M, mean1);
    confwidth1 = confwidth(stddev1, M);
    confmm1 = confmm(mean1, confwidth1);
    confmax1 = confmax(mean1, confwidth1); 
    
    printf("S         %lf\n", S);
    printf("E         %lf\n", E);
    printf("r         %lf\n", r);
    printf("sigma     %lf\n", sigma);
    printf("T         %lf\n", T);
    printf("M         %d\n", M);
    printf("Confidence interval: (%lf, %lf)\n", confmm1, confmax1);
    
    // </editor-fold>
    return (EXIT_SUCCESS);
}

